<?php declare(strict_types=1);

namespace shali\ydb\think2fa;

use shali\ydb\think2fa\libs\PHPGangsta_GoogleAuthenticator;

class Think2Fa
{
    private $client;

    public function __construct()
    {
        $this->client = new PHPGangsta_GoogleAuthenticator();
        // 密码长度设置 6 位，区分短信验证码 4 位
        $this->client->setCodeLength(6);
    }

    /**
     * 生成密钥
     * @return string
     * @throws \Exception
     */
    public function genSecret(int $length = 32): string
    {
        return $this->client->createSecret($length);
    }

    /**
     * @param string $secret 密钥
     * @param string $phone 手机号
     * @param string $platformChineseName 平台中文名称
     * @return string
     */
    public function getQrCode(string $secret, string $phone, string $platformChineseName = ''): string
    {
        return $this->client->getQRCodeGoogleUrl($phone, $secret, $platformChineseName);
    }

    public function verify(string $secret, string $code): bool
    {
        return $this->client->verifyCode($secret, $code);
    }
}
